package com.jtech.together.util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.jtech.together.view.activity.LocationActivity;

import static android.app.Activity.RESULT_OK;

/**
 * 地理位置选择
 * Created by wuxubaiyang on 2017/5/11.
 */

public class LocationSelect {
    public static final int REQUEST_LOCATION = 0x4671;

    /**
     * 位置选择
     *
     * @param activity
     */
    public static void select(Activity activity) {
        ActivityJump.build(activity, LocationActivity.class)
                .jumpForResult(REQUEST_LOCATION);
    }

    /**
     * 显示目标点
     *
     * @param activity
     * @param location
     * @param title
     * @param describe
     */
    public static void showTarget(Activity activity, double[] location, String title, String describe) {
        Bundle bundle = new Bundle();
        bundle.putString(LocationActivity.KEY_LOCATION_TITLE, title);
        bundle.putDoubleArray(LocationActivity.KEY_LOCATION, location);
        bundle.putString(LocationActivity.KEY_LOCATION_DESCRIBE, describe);
        ActivityJump.build(activity, LocationActivity.class)
                .addBundle(bundle)
                .jump();
    }

    /**
     * 判断是否为位置请求的返回
     *
     * @param requestCode
     * @param resultCode
     * @return
     */
    public static boolean isRequestLocationResult(int requestCode, int resultCode) {
        return requestCode == REQUEST_LOCATION && resultCode == RESULT_OK;
    }

    /**
     * 判断是否为位置请求的返回
     *
     * @param requestCode
     * @return
     */
    public static boolean isRequestLocationResult(int requestCode) {
        return requestCode == REQUEST_LOCATION;
    }

    /**
     * 获取请求的位置
     *
     * @param data
     * @return
     */
    public static double[] getRequestLocation(@NonNull Intent data) {
        if (null == data) {
            return null;
        }
        return data.getDoubleArrayExtra(LocationActivity.KEY_LOCATION);
    }

    /**
     * 获取请求的位置描述
     *
     * @param data
     * @return
     */
    public static String getRequestLocationAddress(@NonNull Intent data) {
        if (null == data) {
            return null;
        }
        return data.getStringExtra(LocationActivity.KEY_LOCATION_ADDRESS);
    }
}